package gui

import swing._
import event._
import javax.swing.UIManager
import lang.LOC
import actors._
import actors.Actor._
import java.awt.Color;

case class Surface {
  val mainWindow = new MainFrame()

  def draw {
    mainWindow.title = LOC.get("Titel")
    //mainWindow.iconImage = java.awt.Toolkit.getDefaultToolkit.getImage(resourceFromClassloader("/gui/img/icon.png"))
    mainWindow.minimumSize = new Dimension(800, 600)
    mainWindow.maximize

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    mainWindow.menuBar = new MenuBar {
      val menusize = augmentString(LOC.get("menusize")).toInt
      for (i <- 1 to menusize) {
        contents += new Menu(LOC.get("menu" + i)) {
          var menuitemsize = augmentString(LOC.get("menu" + i + "size")).toInt
          for (j <- 1 to menuitemsize) {
            val tmp = LOC.get("menu" + i + "item" + j)
            if (tmp == "---") {
              contents += new Separator
            } else {
              contents += new MenuItem(Action(tmp) { System.exit(0) })
            }
          }
        }
      }
    }

    val layout = new SplitPane(Orientation.Vertical) {
      dividerLocation = 250
      dividerSize = 7

      val parallelAction = Action("hundred thousand messages") {
        val reciever: Actor = actor { loop { react { case (increment: Int) => pbar.value = increment } } }
        val task = new MyCounter().start
        task ! reciever
      }

      val pbar = new ProgressBar { labelPainted = true; max = 100000; value = 0 }
      val firstpanel = new FlowPanel {
        contents += new Label { text = "A Label" }
        contents += new Button { action = parallelAction }
        contents += new Label { text = "Another Label" }
        contents += pbar
      }

      val secondpanel = new FlowPanel {
        var tA = new TextArea {
          text = "sdsdsds"
          background = Color.WHITE
          rows = 10
          columns = 50
          editable = true
        }
        contents += tA
      }

      leftComponent = firstpanel
      rightComponent = secondpanel
    }

    mainWindow.contents = layout
    mainWindow.visible = true

  }
}

class MyCounter() extends Actor {
  def act() {
    loop {
      react {
        //start action and permanently send feedback
        case (reciever: Actor) => for (i <- 1 to 100000) reciever ! i
      }
    }
  }
}